package com.mornd.gulimall.order.config.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: mornd
 * @dateTime: 2023/8/26 - 7:16
 * 声明订单的队列，交换机，绑定关系
 */
@Slf4j
@Configuration
public class OrderMQConfig {

    /**
     * mq没有该队列就创建，如果mq已经存在该队列，以后该方法配置的属性不会覆盖mq的
     * web管理页面手动删除队列后，绑定关系会自动删除
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        return QueueBuilder.durable("order.delay.queue")
                // 绑定死信交换机
                .deadLetterExchange("order-event-exchange")
                // 路由键
                .deadLetterRoutingKey("order.release.order")
                // 30分钟
                .ttl((int) TimeUnit.MINUTES.toMillis(30))
                .build();

//        Map<String, Object> arguments = new HashMap<String, Object>();
//        arguments.put("x-dead-letter-exchange", "order-event-exchange");
//        arguments.put("x-dead-letter-routing-key", "order.release.order");
//        arguments.put("x-message-ttl", 60000);
//        return new Queue("order.delay.queue", true, false, false, arguments);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order.release.order.queue",
                true, false, false);
    }

    @Bean
    public Exchange orderEventExchange() {
        return ExchangeBuilder.topicExchange("order-event-exchange")
                .durable(true)
                .build();
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        return BindingBuilder.bind(orderDelayQueue())
                .to(orderEventExchange())
                .with("order.create.order")
                .noargs();
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return BindingBuilder.bind(orderReleaseOrderQueue())
                .to(orderEventExchange())
                .with("order.release.order")
                .noargs();
    }

//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(Message msg, Map map, Channel channel) throws IOException {
//        log.debug("收到消息：{}", map);
//        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
//        channel.basicAck(deliveryTag, false);
//    }

    /**
     * 订单过期后还要主动通知库存服务解锁，主动+被动保证库存能正确解锁
     */


    @Bean
    public Queue orderReleaseOtherQueue() {
        return new Queue("order.release.other.queue",
                true, false, false);
    }

    @Bean
    public Binding orderReleaseOtherBinding() {
        return BindingBuilder.bind(orderReleaseOtherQueue())
                .to(orderEventExchange())
                .with("order.release.other.#")
                .noargs();
    }


    /**
     * 秒杀相关，用户秒杀完毕后需要做的事
     */

    @Bean
    public Queue orderSeckillQueue() {
        return new Queue("order.seckill.queue",
                true, false, false);
    }

    @Bean
    public Binding orderSeckillOrderBinding() {
        return BindingBuilder.bind(orderSeckillQueue())
                .to(orderEventExchange())
                .with("order.seckill.order")
                .noargs();
    }

}
